package com.cqc.appmanager01;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.text.format.Formatter;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 1 获取手机和sd卡可用空间
 * 2 获取手机中安装的app，并区分哪些是系统应用，哪些是用户安装
 * 3 listView的复杂显示
 */
public class MainActivity extends AppCompatActivity {

    private Context context = this;
    private List<AppInfo> appList = new ArrayList<>();
    private List<AppInfo> sysAppList = new ArrayList<>();
    private List<AppInfo> userAppList = new ArrayList<>();

    @BindView(R.id.layout_loading)
    LinearLayout layoutLoading;
    @BindView(R.id.tv_phone_size)
    TextView tvPhoneSize;
    @BindView(R.id.tv_sd_size)
    TextView tvSdSize;
    @BindView(R.id.listView)
    ListView listView;
    @BindView(R.id.layout_content)
    LinearLayout layoutContent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        setSize();//获取手机和sd卡剩余空间

        new AsyncTask<Void, Void, Void>() {
            @Override
            protected void onPreExecute() {
                layoutContent.setVisibility(View.GONE);
                super.onPreExecute();
            }

            @Override
            protected Void doInBackground(Void... params) {
                getAppsList();
                for (AppInfo appInfo : appList) {
                    if (appInfo.isSys()) {
                        sysAppList.add(appInfo);
                    } else {
                        userAppList.add(appInfo);
                    }
                }
                return null;
            }

            @Override
            protected void onPostExecute(Void aVoid) {
                super.onPostExecute(aVoid);
                layoutLoading.setVisibility(View.GONE);
                layoutContent.setVisibility(View.VISIBLE);

                MyAdapter adapter = new MyAdapter(sysAppList, userAppList);
                listView.setAdapter(adapter);
            }
        }.execute();
    }

    private void setSize() {
        long phoneFreeSpaceL = Environment.getDataDirectory().getFreeSpace();
        long sdFreeSpaceL = Environment.getExternalStorageDirectory().getFreeSpace();
        String phoneFreeSpace = Formatter.formatFileSize(context, phoneFreeSpaceL);
        String sdFreeSpace = Formatter.formatFileSize(context, sdFreeSpaceL);
        tvPhoneSize.setText("手机剩余：" + phoneFreeSpace);
        tvSdSize.setText("sd卡剩余：" + sdFreeSpace);
    }

    public void getAppsList() {
        PackageManager pm = getPackageManager();
        List<PackageInfo> packages = pm.getInstalledPackages(0);//直接写也可以PackageManager.GET_INSTRUMENTATION
        for (PackageInfo packageInfo : packages) {
            ApplicationInfo info = packageInfo.applicationInfo;
            Drawable icon = info.loadIcon(pm);
            String label = info.loadLabel(pm).toString();
            String sourceDir = info.sourceDir;
            String packageName = packageInfo.packageName;
            /**
             * String name = info.name;//null
             * sourceDir=/data/app/so.ofo.labofo-1/base.apk
             * label=ofo共享单车
             * packageName=so.ofo.labofo
             */
            //            LogUtil.d("sourceDir=" + sourceDir + ",label=" + label + ",packageName=" + packageName);


            AppInfo appInfo = new AppInfo();
            appInfo.setLabel(label);
            appInfo.setSourceDir(sourceDir);
            appInfo.setIcon(icon);

            File apkFile = new File(sourceDir);
            appInfo.setSize(apkFile.length());

            if ((info.flags & ApplicationInfo.FLAG_SYSTEM) == ApplicationInfo.FLAG_SYSTEM) {
                appInfo.setSys(true);
            } else {
                appInfo.setSys(false);
            }

            if ((info.flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) == ApplicationInfo.FLAG_EXTERNAL_STORAGE) {
                appInfo.setInPhone(false);
            } else {
                appInfo.setInPhone(true);
            }
            appList.add(appInfo);
        }
    }
}
